import java.util.ArrayList;
import java.util.List;

// bfs
class Solution_310 {
    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        int min = Integer.MAX_VALUE;
        List<Integer> result = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            int h = TreeHeight(i, n, edges);
            System.out.println(h);
            
            if( min > h) {
                min = h;
                result = new ArrayList<Integer>();
            }

            if(min == h) {
                result.add(i);
            }
        }
        return result;
    }

    public int TreeHeight(int v, int n, int[][] edges) {
        List<Integer> queue = new ArrayList<Integer>();
        boolean[] isVisited = new boolean[n];
        int height = 0;
        queue.add(v);
        isVisited[v] = true;
        while(!queue.isEmpty()) {
            for(int i = 0; i < edges.length; i++) {
                for(int j = 0; j < 2; j++) {
                    if(v == edges[i][j]) {
                        int next = edges[i][(j + 1) % 2];
                        if (!isVisited[next]) {
                            queue.add(next);
                            isVisited[next] = true;
                        }
                       
                    }
                }
            }
            queue.remove(0);
            height += 1;
        }
        return height;
    }
}

// 拓扑排序
